//   Copyright 2009 Pyxis Technologies
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//

using Pyxis.FluentSelenium.Conditions;

namespace Pyxis.FluentSelenium.Locator
{
    public class ComboboxLocator : ILocator
    {
        private readonly IWaitCondition postSelectWaitCondition;
        private string locator;

        public IWaitCondition PostSelectWaitCondition
        {
            get { return postSelectWaitCondition; }
        }

        protected ComboboxLocator(string locator)
        {
            this.locator = locator;
            this.postSelectWaitCondition = new EmptyWaitCondition();
        }

        protected ComboboxLocator(string locator, IWaitCondition postSelectWaitCondition) : this(locator)
        {
            this.locator = locator;
            this.postSelectWaitCondition = postSelectWaitCondition;
        }

        public static ComboboxLocator WithId(string id)
        {
            return new ComboboxLocator(XPathHelper.BuildLocatorForId(id));
        }

        public static ComboboxLocator Contains(string fragment)
        {
            return new ComboboxLocator(XPathHelper.BuildLocatorForWithContaining(fragment));
        }

        public static ComboboxLocator WithXpath(string xpathLocator)
        {
            return new ComboboxLocator(xpathLocator);
        }

        public static ComboboxLocator WithId(string id, IWaitCondition postSelectWaitCondition)
        {
            return new ComboboxLocator(XPathHelper.BuildLocatorForId(id), postSelectWaitCondition);
        }

        public static ComboboxLocator Contains(string fragment, IWaitCondition postSelectWaitCondition)
        {
            return new ComboboxLocator(XPathHelper.BuildLocatorForWithContaining(fragment), postSelectWaitCondition);
        }

        public static ComboboxLocator WithXpath(string xpathLocator, IWaitCondition postSelectWaitCondition)
        {
            return new ComboboxLocator(xpathLocator, postSelectWaitCondition);
        }

        public void Append(ILocator parentLocator)
        {
            locator = parentLocator + locator;
        }

        public override string ToString()
        {
            return locator;
        }
    }
}